import argparse
import urllib
from pyquery import PyQuery as pq
def getArgs():
    parse=argparse.ArgumentParser()
    parse.add_argument('-u',type=str)
    parse.add_argument('-d',type=str)
    parse.add_argument('-o',type=str)
    args=parse.parse_args()
    return vars(args)
def urlAddQuery(url,query):
    query=query.replace(',','&') 
    if '?' in url:
        return url.replace('?','?'+query+'&')
    else:
        return url+'?'+query
def getHref():
    args=getArgs()
    url=args['u']
    query=args['d'].strip("\'")
    fileName=args['o']
    doc=pq(url=url)
    with open(fileName,'w') as f:
        for a in doc('a'):
            a=pq(a)
            href=a.attr('href')
            if href:
                newurl=urlAddQuery(href,query)
                f.write(newurl+'\n')
if __name__=='__main__':
    getHref()

